package com.xuchen.zhenyang.controller;


import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.xuchen.zhenyang.base.BaseQuery;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.controller.base.BaseController;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.CustomerText;
import com.xuchen.zhenyang.entity.base.MyEntityWrapper;
import com.xuchen.zhenyang.service.CustomerTextService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 自定义文本 前端控制器
 * </p>
 *
 * @author xuchen
 * @since 2019-10-27
 */
@Controller
@RequestMapping("/customerText")
@RequiresPermissions("customerText")
@Slf4j
public class CustomerTextController extends BaseController {

    @Autowired
    CustomerTextService customerTextService;

    @GetMapping("")
    String index(HttpServletRequest request) {
        setAttributeEnums(request);
        return "customer/text/customer-text-list";
    }

    @GetMapping("getById")
    @ResponseBody
    CustomerText index(String id) {
        return customerTextService.selectById(id);
    }

    @GetMapping("list")
    @ResponseBody
    Result list(BaseQuery baseQuery, CustomerText myEntity, String params) {
        if (StrUtil.isNotBlank(params)) {
            myEntity = JSONObject.parseObject(params).toJavaObject(CustomerText.class);
        }
        MyEntityWrapper<CustomerText> wrapper = new MyEntityWrapper<>(baseQuery, myEntity);
        wrapper.like("title");
        List<CustomerText> list = customerTextService.selectList(wrapper);
        return Result.success(PageHelper.freeTotal(), list);
    }

    @GetMapping("toAdd")
    String toAdd(HttpServletRequest request) {
        setAttributeEnums(request);
        return "customer/text/customer-text-add";
    }

    @PostMapping("doAdd")
    @ResponseBody
    @RequestLog
    Result doAdd(CustomerText myEntity) {
        myEntity.setCreateUser(getSessionUserName());
        myEntity.setCreateTime(new Date());
        customerTextService.insert(myEntity);
        myEntity.setUrl(textDomain+"text.html?id=" + myEntity.getId());
        customerTextService.updateById(myEntity);
        return Result.success();
    }

    @GetMapping("toEdit")
    String toEdit(CustomerText myEntity, HttpServletRequest request) {
        setAttributeEnums(request);
        request.setAttribute("myEntity", customerTextService.selectById(myEntity));
        return "customer/text/customer-text-edit";
    }

    @PostMapping("doEdit")
    @ResponseBody
    @RequestLog
    Result doEdit(CustomerText myEntity) {
        myEntity.setUpdateTime(new Date());
        myEntity.setUpdateUser(getSessionUserName());
        myEntity.setUrl(textDomain + "text.html?id=" + myEntity.getId());
        customerTextService.updateById(myEntity);
        return Result.success();
    }

    @GetMapping("toQrCode")
    String toQrCode(CustomerText myEntity, HttpServletRequest request) {
        setAttributeEnums(request);
        request.setAttribute("myEntity", customerTextService.selectById(myEntity));
        return "customer/text/qr-code";
    }

    @GetMapping(value = "doQrCode")
    @ResponseBody
    @RequestLog
    void doQrCode(CustomerText myEntity) throws IOException {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        QrConfig qrConfig = new QrConfig(myEntity.getSize(),myEntity.getSize());
        qrConfig.setImg(ImgUtil.read(new URL("http://img.zhenyang.work/logo.jpg")));
        response.setHeader("content-Type","application/x-png");
        response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode("二维码.png", StandardCharsets.UTF_8.name()));
        QrCodeUtil.generate(textDomain + "text.html?id=" + myEntity.getId(), qrConfig,ImgUtil.IMAGE_TYPE_PNG,response.getOutputStream());
    }

    @PostMapping("delete")
    @ResponseBody
    @RequestLog
    Result delete(CustomerText myEntity) {
        customerTextService.deleteById(myEntity);
        return Result.success();
    }

    private void setAttributeEnums(HttpServletRequest request) {
    }
}

